package main

import (
	"errors"
	"os/exec"
	"strings"
)

type WifiInfo struct {
	Inuse  string `json:"inuse"`  // 当前是否使用
	BSSID  string `json:"bssid"`  // 物理地址址
	SSID   string `json:"ssid"`   // 名称
	Signal string `json:"signal"` // 信号强度（越大越好）
}

// GetInUseWifiList 获取可用WIFI列表
func GetInUseWifiList() (wifiList []WifiInfo, e error) {
	// 要执行的命令
	cmd := exec.Command("nmcli", "device", "wifi", "list")

	// 执行命令并获取输出
	output, err := cmd.Output()
	if err != nil {
		e = err
		return
	}
	str := string(output)
	//fmt.Println(str)
	lines := strings.Split(str, "\n")

	bssidPos := strings.Index(lines[0], "BSSID")
	ssidS := strings.LastIndex(lines[0], "SSID")
	ssidE := strings.Index(lines[0], "MODE")
	signalS := strings.Index(lines[0], "SIGNAL")
	signalE := strings.Index(lines[0], "BARS")
	tmpWifi := WifiInfo{}
	for i, v := range lines {
		//fmt.Println(v)
		if i > 0 && i < len(lines)-1 {
			if len(v) <= 0 {
				continue
			}
			if string(v[0]) == "I" {
				if string(v[0]) == "I" {
					bssidPos = strings.Index(lines[i], "BSSID")
					ssidS = strings.LastIndex(lines[i], "SSID")
					ssidE = strings.Index(lines[i], "MODE")
					signalS = strings.Index(lines[i], "SIGNAL")
					signalE = strings.Index(lines[i], "BARS")
				}
				continue
			}
			tmpWifi.Inuse = string(v[0])

			if bssidPos > 0 {
				tmpWifi.BSSID = strings.TrimSpace(v[bssidPos : ssidS-1])
			}

			tmpWifi.SSID = strings.TrimSpace(v[ssidS : ssidE-1])
			tmpWifi.Signal = strings.TrimSpace(v[signalS : signalE-1])
			//fmt.Println(tmpWifi.BSSID, tmpWifi.SSID, tmpWifi.Signal)
			wifiList = append(wifiList, tmpWifi)
		}

	}
	return
}

// ConnectWifi 连接WIFI
func ConnectWifi(ssid, pwd string) (e error) {
	var list []string
	list, e = GetConnectedList()
	if e != nil {
		return
	}
	if e = DeleteWifi(list); e != nil {
		return
	}
	cmd := exec.Command("sudo", "nmcli", "device", "wifi", "connect", ssid, "password", pwd)

	// 执行命令并获取输出
	output, err := cmd.Output()
	if err != nil {
		e = err
		return
	}
	str := string(output)
	if strings.Contains(str, "successfully") {
		return
	}
	e = errors.New(str)
	return
}

// GetConnectedList 获取连接过的WIFI
func GetConnectedList() (wifiList []string, e error) {
	// 要执行的命令
	cmd := exec.Command("sudo", "nmcli", "connection", "show")

	// 执行命令并获取输出
	output, err := cmd.Output()
	if err != nil {
		e = err
		return
	}
	str := string(output)
	//fmt.Println(str)
	lines := strings.Split(str, "\n")

	nameS := strings.LastIndex(lines[0], "NAME")
	nameE := strings.Index(lines[0], "UUID")
	typeS := strings.Index(lines[0], "TYPE")
	typeE := strings.Index(lines[0], "DEVICE")
	for i, v := range lines {
		if i > 0 && i < len(lines)-1 {
			tType := strings.TrimSpace(v[typeS : typeE-1])
			if tType == "wifi" {
				wifiList = append(wifiList, strings.TrimSpace(v[nameS:nameE-1]))
			}
		}
	}
	return
}

// DeleteWifi 删除连接过的WIFI列表
func DeleteWifi(ssid []string) (e error) {
	for _, v := range ssid {
		cmd := exec.Command("sudo", "nmcli", "connection", "delete", v)
		// 执行命令并获取输出
		output, err := cmd.Output()
		if err != nil {
			e = err
			return
		}
		str := string(output)
		if strings.Contains(str, "successfully") {
			continue
		}
		e = errors.New(str)
		return
	}
	return
}

func SearchWifi() (e error) {
	cmd := exec.Command("sudo", "nmcli", "device", "wifi", "rescan")

	// 执行命令并获取输出
	e = cmd.Run()
	return
}
